<%@ page language="java" pageEncoding="UTF-8" %>
<% 
    String context = request.getContextPath();
    request.setAttribute("context",context);
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>用户</title>
    <link href="${context}/static/jquery-easyui-1.3.2/themes/gray/easyui.css" rel="stylesheet" type="text/css" />
    <link href="${context}/static/jquery-easyui-1.3.2/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="${context}/static/jquery-easyui-1.3.2/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="${context}/static/jquery-easyui-1.3.2/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="${context}/static/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="${context}/static/scripts/json2.js" type="text/javascript"></script>
    <script src="${context}/static/scripts/common.js" type="text/javascript"></script> 
    <style type="text/css">
        body {
            padding: 0px;
            margin: 0px;
        }
    </style>
    <script type="text/javascript">
    	var nameMap={"BUG追踪":"3","工作计划":"1","学习计划":"2","知识点":"4"};
    	var statusMap={"1":"完成","2":"未完成","3":"废弃"};
    	var currentRowId=null;
    	var currentRow=null;
    
        $(document).ready(function () {
            $("#Explorer").accordion({
                fit: true,
                animate: false,
                onSelect: function(title) {
                	loadRightGrid();
                }
            }); 
            initRightGrid();
            loadRightGrid();
        });
        	
		function uploadComplete(result) {
			alert(JSON.stringify(result));
			var files=result.files;
			for(var i=0;i<files.length;i++) {
				var fileObj=JSON.parse(files[i].file);
				fileObj.appid=currentRowId;
				fileObj.createuser="duankaige";
				var p=getParmeterOfObj(fileObj);	
				myAjax("${context}/upload/addFile.html",p, $("#frameMain")[0].contentWindow.loadRightGrid(null,currentRowId));
			}
		}		
		
		
		function initRightGrid() {
		    $("#dataGrid").datagrid({
		        fit: true,
		        nowrap: true,
		        fitColumns: true,
		        rownumbers: true,
		        singleSelect: false,
		        striped: true,
		        pagination:true,
		        columns: [[
			        {"field":"title","title":"标题","width":80,"align":"left"},
			        {"field":"content","title":"内容","width":210,"align":"left"},
			        {"field":"createTime","title":"创建时间","width":80,"align":"left","formatter":function(value,rec) {
		            	var result=new Date(value).format("yyyy-MM-dd hh:mm:ss");
						return result;
					}},
			        {"field":"status","title":"状态","width":40,"align":"left","formatter":function(value,rec) {
		            	var result=statusMap[value];
						return result;
					}},
			        {"field":"type","title":"类型","width":40,"align":"left","formatter":function(value,rec) {
		            	var result=value;
						for(var i in nameMap) {
							if(nameMap[i]==value) {
								result=i;
							}
						}
						return result;
					}},
			        {"field":"remark","title":"备注","width":80,"align":"left"},
			        {"field":"worker","title":"负责人","width":80,"align":"left"},
			        {"field":"docmanage","title":"文档管理","width":80,"align":"left",formatter: function (value, rowData, rowIndex) {
		             	return "<a href='javascript:void(0);' onclick='upload(\"" + rowData.id + "\");'>上传新文档</a>"; 
		            }}
		        ]],
				onClickRow: function (row) {
					var _currentRow=$("#dataGrid").datagrid("getRows")[row];
					currentRow=_currentRow;
					$("#frameMain")[0].contentWindow.loadRightGrid(null,_currentRow.id);
				}, 
				onDblClickRow: function (rowIndex, field, value) {	
					currentRow=field;
					editRight();		
				}, 
				onAfterEdit: function (rowIndex, rowData, changes) {
				}, 
				onLoadSuccess: function (data) {
					currentRow=null;		
				}
		    });     
		    
		    //分页控件设置
		    var pager = $("#dataGrid").datagrid("getPager");
		    pager.pagination({
		        onSelectPage: function (pageNumber, pageSize) {
		        	var option = $("#dataGrid").datagrid("options");//确保第二页序号不从1开始
		            option.pageSize = pageSize;
		            option.pageNumber = pageNumber;
		        	var param={};
		            pageNumber = parseInt(pageNumber, 10);
		            pageSize = parseInt(pageSize, 10);
		            param.page=pageNumber;
		            param.size=pageSize;
		            param.type=nameMap[$("#Explorer").accordion("getSelected").panel('options').title];
		            loadRightGrid(param);
		        }
		    }); 
		    
		}		
		
	    function upload(id) {
	    	currentRowId=id;
		    var _currentDialog=OpenDialog({
		        "width": 400,
		        "height": 300,
		        "title": "用户",
		        "cancelButton": false,
		        "url": "${context}/jsp/common/upload.jsp",
		        "isFrame":true,
		        "buttons": [
		        ] 
		    });			    	
	    }	
	    
	    	
		function loadRightGrid(paramPager) {
			var pager=$("#dataGrid").datagrid("getPager");
			if(!paramPager) paramPager={"page":1,"size":parseInt(pager.pagination("options").pageSize, 10),"type":nameMap[$("#Explorer").accordion("getSelected").panel('options').title]};
		    $.ajax({
		        url: "${context}/project/getPagerList.html",
		        data: paramPager,
		        dataType: "json",
		        type: "GET",
		        async: false,
		        success: function (result) {
		        	var rows=result.rows;
		            $("#dataGrid").datagrid("loadData", { total: result.total, rows: result.rows });
		            $("#dataGrid").datagrid("loaded");
		        }
		    });	
		}         
        
        function getFormReturnObj(win,formId) {
			var objStr=win.$("#"+formId).serialize();
        	var obj={};
        	var objStrArray=objStr.split("&");
        	for(var i=0;i<objStrArray.length;i++) {
        		var temp=objStrArray[i];
        		var arrs=temp.split("=");
                obj[arrs[0]]=arrs[1];
        	}   
        	return obj;     
        }
        
        function getParmeterOfObj(obj) {
            var objArray=[];
            for(var key in obj) {
                var s=key+"="+obj[key];
                objArray.push(s);
            }
            var parmeter=objArray.join("&"); 
            return parmeter;       
        }
        
        
		function addRight() {
			var currentType=nameMap[$("#Explorer").accordion("getSelected").panel('options').title];
		    var _currentDialog=OpenDialog({
		        "width": 900,
		        "height": 600,
		        "title": "用户",
		        "cancelButton": true,
		        "url": "${context}/jsp/view.html?p=project_client/addworkproject",
		        "isFrame":true,
		        "buttons": [{ "text": "确定", "iconCls": "icon-ok", "handler": function() {
	                var iframe = _currentDialog.find("iframe");
	                var win = iframe.get(0).contentWindow;
		        	var obj=getFormReturnObj(win,"form");
		        	obj.type=nameMap[$("#Explorer").accordion("getSelected").panel('options').title];
					var parmeter=getParmeterOfObj(obj);
				    $.ajax({
				        url: "${context}/project/save.html",
				        data: parmeter,
				        dataType: "json",
				        type: "GET",
				        async: false,
				        success: function (result) {
							loadRightGrid();
				        }
				    });	   
				    _currentDialog.dialog("close");
		        }
		        }] 
		    });	
		}
		
		function editRight() {
		    var _currentDialog=OpenDialog({
		        "width": 900,
		        "height": 600,
		        "title": "编辑",
		        "cancelButton": true,
		        "dialogArgs": currentRow,
		        "url": "${context}/jsp/view.html?p=project_client/addworkproject",
		        "isFrame":true,
		        "buttons": [{ "text": "确定", "iconCls": "icon-ok", "handler": function() {
	                var iframe = _currentDialog.find("iframe");
	                var win = iframe.get(0).contentWindow;
		        	var obj=getFormReturnObj(win,"form");
		        	obj.type=nameMap[$("#Explorer").accordion("getSelected").panel('options').title];
					var parmeter=getParmeterOfObj(obj);
				    $.ajax({
				        url: "${context}/project/save.html",
				        data: parmeter,
				        dataType: "json",
				        type: "POST",
				        async: false,
				        success: function (result) {
							loadRightGrid();
							 _currentDialog.dialog("close");
				        }
				    });	   
		        }
		        }] 
		    });	
		}
		
		function deleteRight() {
			var rows=$("#dataGrid").datagrid("getSelections");
			var rowIds=[];
			for(var i=0;i<rows.length;i++) {
				rowIds.push(rows[i].id);
			}
			if(!rows) return;
		    $.messager.confirm("删除数据", "您确认要删除选中数据吗？", function (result) {
		        if (result) {
				    $.ajax({
				        url: "${context}/project/delete.html",
				        data: {"ids":rowIds.join()},
				        dataType: "json",
				        type: "POST",
				        async: false,
				        success: function (result) {
							loadRightGrid();
				        }
				    });						
		        }
		    });				
		
		}
		
        
    </script>
</head>
<body>
    <div class="easyui-layout" style="width: 100%; height: 100%;">
        <div region="west" split="true" title="项目管理工具" style="width: 220px; overflow: auto; padding-top: 0px;">
            <div id="Explorer" class="easyui-accordion" border="false">
                <div title="工作计划" iconcls="icon-edit" style="overflow: auto; padding: 10px;">
                </div>
                <div title="学习计划" iconcls="icon-edit" style="overflow: auto; padding: 10px;">
                </div>
                <div title="知识点" iconcls="icon-edit"  style="overflow: auto; padding: 10px;">
                </div>
                <div title="BUG追踪" iconcls="icon-edit" style="padding: 10px;">
                </div>
            </div>
        </div>
        <div region="center" border="false" style="overflow: hidden;">
            <div class="easyui-layout" style="width: 100%; height: 100%;" fit="true">
                <div region="north" border="false" split="false" style="height: 32px; overflow: auto; padding-top: 0px; background-color: #e9f1fe;">
                    <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; font-size: 12px; border-bottom: 1px solid #99bbe8;">
                        <tr>
                            <td>
                                <div id="GridButtonsPanel" style="padding: 2px 5px 2px 5px;float:right">
                                    <a id="addRight"  href="javascript:void(0)" class="easyui-linkbutton" plain="true" onClick="addRight();return false;" iconcls="icon-addfile" style="float: left">新增</a>
                                    <a id="updateRight"  href="javascript:void(0)" class="easyui-linkbutton" plain="true" onClick="editRight();return false;" iconcls="icon-updatefile" style="float: left">修改</a>
                                    <a id="deleteRight"  href="javascript:void(0)" class="easyui-linkbutton" plain="true" onClick="deleteRight();return false;" iconcls="icon-deletefile" style="float: left">删除</a>
                                </div>
                            </td>
                        </tr>
                    </table>
                </div>
                <div region="center" border="false" style="overflow: hidden;">
                    <table id="dataGrid" border="false">
                    </table>
                </div>
		      <div region="south" title="文档列表" style="height:300px;" border="false">
		          <iframe id="frameMain" name="frameMain" width="100%" height="100%" scrolling="no" frameborder="0" style="border: 0px;" src="${context}/jsp/view.html?p=project_client/uploadManage"></iframe>
		      </div>                
            </div>
           </div>
    </div>
</body>
</html>
